<template>
    <div class="u_select" :style="selectStyle" ref="target">
        <div class="selected-option" @click="showOptions">
            <span v-if="localValue && !multiple">{{ localValue }}</span>
            <span v-if="localValue && multiple">{{ localValue.join(',') }}</span>
            <span v-if="!localValue || localValue.length == 0">{{ placeholder }}</span>
            <img v-if="!show" class="icon" src="@/assets/images/icon_up.png">
            <img v-else class="icon" src="@/assets/images/icon_down.png">
        </div>
        <ul class="options-list" v-show="show">
            <div v-if="multiple">
                <li v-for="option in options" :key="option"
                    :class="['option', localValue.indexOf(option) != -1 && 'active']" @click="getOption(option)">
                    {{ option }}
                </li>
            </div>
            <div v-else>
                <li v-for="option in options" :key="option" :class="['option', localValue == option && 'active']"
                    @click="getOption(option)">
                    {{ option }}
                </li>
            </div>
        </ul>
    </div>
</template>
<script setup>
import { getCurrentInstance, ref, computed } from "vue";
import { onClickOutside } from "@vueuse/core";
const { proxy } = getCurrentInstance();

const props = defineProps({
    width: {//宽度
        type: [String],
        default: '100%'
    },
    options: {//选项列表
        type: Array
    },
    placeholder: {
    },
    bgColor: {//背景颜色
        type: String,
        default: "rgba(33, 224, 197, 0.12)"
    },
    multiple: {
        type: Boolean,
        default: false
    },
    modelValue: {
        type: [Array, String, Boolean]
    },
    disabled: {
        type: Boolean,
        default: false
    }
});
// 组装样式
const selectStyle = {
    width: props.width,
};
// 控制显示下拉框
const show = ref(false);
// 获取本组件实例
const target = ref();
// 选中的值，其中的判断是看父组件是否传递有默认展示的值，如果没有就默认展示option数组的第一个值
const localValue = computed({
    get() {
        return props.modelValue;
    },
    set(newValue) {
        proxy.$emit('update:modelValue', newValue);
    }
});

// 在组件范围之外的点击事件
onClickOutside(target, () => {
    // 在组件范围之外点击，关闭显示下拉框
    show.value = false;
})
// 下拉框点击事件
const getOption = (option) => {
    if (props.multiple) {
        if (localValue.value.indexOf(option) == -1) {
            localValue.value.push(option);
        } else {
            localValue.value = localValue.value.filter((item) => item != option);
        }
    } else {
        // 修改展示的值
        localValue.value = option;
    }
    // 关闭显示下拉框
    show.value = false;
}
function showOptions() {
    if (props.disabled) {
        return;
    }
    show.value = !show.value;
}
</script>

<style lang="scss" scoped>
.u_select {
    display: inline-block;
    border-radius: 4px;
    color: #FFF;
    position: relative;
    opacity: 1;
    background: rgba(33, 224, 197, 0.12);
    border: 1.5px solid rgba(33, 224, 197, 0.5);
    font-size: 32px;

    .selected-option {
        height: 62px;
        padding-left: 10px;
        padding-right: 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        cursor: pointer;

        span {
            flex-shrink: 0;
        }

        .icon {
            width: 26px;
            height: 26px;
            min-width: 15px;
            min-height: 15px;
            margin-left: 20px;
        }
    }

    .options-list {
        max-height: 400px;
        overflow-y: auto;
        position: absolute;
        bottom: 60px;
        left: 0;
        width: 100%;
        padding: 0;
        margin: 0;
        z-index: 200;
        border-radius: 6px;
        transition: all 1s ease-in-out;
        background: linear-gradient(0deg, rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.6)), rgba(12, 111, 111, 0.8);
        box-sizing: border-box;
        border: 5px solid rgba(33, 224, 197, 0.5);

        &::-webkit-scrollbar {
            width: 0;
        }
    }

    .option {
        padding: 10px;
        cursor: pointer;
        list-style: none;
        border-bottom: 1px solid #21E0C5;
    }

    .option:hover {
        background-color: #0F4C64;
        color: #fff;
    }

    .active {
        background-color: #0F4C64;
        color: #fff;
    }
}
</style>